package com.b2b.controller;


import com.alibaba.fastjson.JSONObject;
import com.b2b.exception.CustomException;
import com.b2b.model.GoodsDto;
import com.b2b.model.OrdersListDto;
import com.b2b.model.ServiceDto;
import com.b2b.model.common.ResponseBean;
import com.b2b.model.entity.Orders;
import com.b2b.model.entity.OrdersGoods;
import com.b2b.model.entity.Service;
import com.b2b.model.entity.goods.Goods;
import com.b2b.model.entity.goods.Model;
import com.b2b.model.entity.goods.Specification;
import com.b2b.service.*;
import com.b2b.service.goodsService.CategoryService;
import com.b2b.service.goodsService.GoodsService;
import com.b2b.util.GoodsUtil;
import com.b2b.util.ServiceUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdministratorController {

    @Autowired
    ServiceUtil serviceUtil;

    @Autowired
    GoodsService goodsService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    ShopService shopService;

    @Autowired
    ServiceService serviceService;

    @Autowired
    GoodsUtil goodsUtil;

    @Autowired
    OrdersService ordersService;

    @Autowired
    OrdersGoodsService ordersGoodsService;

    @Autowired
    MasterOrderService masterOrderService;

    @Autowired
    CompanyInfoService companyInfoService;
    @GetMapping("/goodsList")
    public ResponseBean goodsList(@RequestParam Map<String, Object> map){
        int page, rows;
        if(!map.containsKey("page")){
            page = 1;
        }else {
            page = Integer.parseInt(map.get("page").toString());
        }
        if(!map.containsKey("rows")){
            rows = 10;
        }else {
            rows = Integer.parseInt(map.get("rows").toString());
        }
        PageHelper.startPage(page, rows);

        char state;

        if(!map.containsKey("state")){
            state = '0';
        }else {
            state = map.get("state").toString().charAt(0);
        }
        Goods goods = new Goods();
        goods.setState(state);
        List<Goods> goodsList = goodsService.select(goods);

        List<GoodsDto> goodsDtoList = new ArrayList<>();
        for (Goods g:goodsList
        ) {
            GoodsDto goodsDto = new GoodsDto();
            goodsDto.setGoodsId(g.getGoodsId());
            goodsDto.setGoodsName(g.getGoodsName());
            goodsDto.setShopId(g.getShopId());
            goodsDto.setShopName(shopService.selectByPrimaryKey(g.getShopId()).getShopName());
            goodsDto.setGoodsImgUrl(JSONObject.parseArray(g.getGoodsImgUrlList()).getString(0));
            goodsDto.setCategoryName(categoryService.selectByPrimaryKey(g.getCategoryId()).getCategoryName());
            goodsDto.setEvaluate(g.getEvaluate());
            goodsDto.setSoldTimes(g.getSoldTimes());
            goodsDto.setState(g.getState());

            goodsDtoList.add(goodsDto);
        }

        PageInfo<GoodsDto> selectPage = new PageInfo<>(goodsDtoList);
        Map<String, Object> result = new HashMap<>();
        result.put("count", selectPage.getTotal());
        result.put("goodsList", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }
    @GetMapping("/goodsDetail")
    public ResponseBean goodsDetail(@RequestParam Integer goodsId){
        Goods goods = goodsService.selectByPrimaryKey(goodsId);

        GoodsDto goodsDto = new GoodsDto();
        goodsDto.setGoodsId(goods.getGoodsId());
        goodsDto.setGoodsName(goods.getGoodsName());
        goodsDto.setShopId(goods.getShopId());
        goodsDto.setShopName(shopService.selectByPrimaryKey(goods.getShopId()).getShopName());
        goodsDto.setSoldTimes(goods.getSoldTimes());
        goodsDto.setEvaluatedTimes(goods.getEvaluatedTimes());
        goodsDto.setEvaluate(goods.getEvaluate());
        goodsDto.setGoodsImgUrlList(JSONObject.parseArray(goods.getGoodsImgUrlList(), String.class));
        goodsDto.setGoodsInfoUrlList(JSONObject.parseArray(goods.getGoodsInfoUrlList(), String.class));
        goodsDto.setGoodsAttributeMap(JSONObject.parseObject(goods.getGoodsAttributeMap()));
        goodsDto.setGoodsSpecificationMap(goodsUtil.getGoodsSpecificationMap(goodsId));

        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", goodsDto);
    }


    @GetMapping("/serviceList")
    public ResponseBean serviceList(@RequestParam Map<String, Object> map){
        int page, rows;
        page = Integer.parseInt(map.get("page").toString());
        rows = Integer.parseInt(map.get("rows").toString());
        PageHelper.startPage(page, rows);
        List<Service> serviceList = serviceService.selectAll();
        List<ServiceDto> serviceDtoList = new ArrayList<>();
        for (Service s: serviceList
             ){
            Orders orders = ordersService.selectByPrimaryKey(s.getOrderId());
            ServiceDto serviceDto = serviceUtil.getServiceDto(s);
            serviceDto.setSellerId(orders.getSellerId());
            serviceDto.setBuyerId(orders.getBuyerId());
            OrdersGoods ordersGoods = new OrdersGoods();
            ordersGoods.setOrderId(s.getOrderId());
            ordersGoods.setStockId(s.getStockId());
            ordersGoods = ordersGoodsService.selectOne(ordersGoods);
            serviceDto.setRefundAmount(ordersGoods.getPrice() * ordersGoods.getNumber());
            serviceDtoList.add(serviceDto);
        }
        PageInfo<ServiceDto> selectPage = new PageInfo<>(serviceDtoList);
        Map<String, Object> result = new HashMap<>();
        result.put("count", selectPage.getTotal());
        result.put("serviceList", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);

    }

    @PutMapping("/passGoods")
    public ResponseBean passGoods(@RequestBody Goods goods){
        Goods goodsTemp = new Goods();
        goodsTemp.setGoodsId(goods.getGoodsId());
        if (goodsService.selectCount(goodsTemp)<=0){
            throw new CustomException("商品不存在");
        }
        goodsTemp.setState('1');
        goodsService.updateByPrimaryKeySelective(goodsTemp);
        return new ResponseBean(HttpStatus.OK.value(), "修改成功(Update successful)", null);
    }

    @DeleteMapping("/rejectGoods")
    public ResponseBean rejectGoods(@RequestParam Integer goodsId) throws IOException {
        if (goodsId != null) {
            if (goodsUtil.hasGoods(goodsId)) {
                goodsUtil.deleteAll(goodsId);
            } else {
                return new ResponseBean(400, "商品不存在", null);
            }
        } else {
            return new ResponseBean(400, "请选择要删除的商品", null);
        }
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Delete Success)", null);
    }

//    @GetMapping("/ordersList")
//    public ResponseBean getOrdersList(@RequestParam Map<String, Object> map){
//        int page = 1, rows = 10;
//        if(map.containsKey("page") && map.get("page").toString().matches("[0-9]+")){
//            page = Integer.parseInt(map.get("page").toString());
//        }
//        if(map.containsKey("rows") && map.get("rows").toString().matches("[0-9]+")){
//            rows = Integer.parseInt(map.get("rows").toString());
//        }
//        List<Orders> ordersList = ordersService.selectAll();
//        List<OrdersListDto> ordersListDtoList = new ArrayList<>();
//        for (Orders o: ordersList
//        ) {
//            OrdersListDto ordersListDto = new OrdersListDto();
//            ordersListDto.setBuyerId(o.getBuyerId());
//            ordersListDto.setBuyerName(companyInfoService.selectByPrimaryKey(o.getBuyerId()).getCompanyName());
//            ordersListDto.setSellerId(o.getSellerId());
//            ordersListDto.setSellerName(companyInfoService.selectByPrimaryKey(o.getSellerId()).getCompanyName());
//            ordersListDto.setOrderId(o.getOrderId());
//            ordersListDto.setOrderTime(o.getOrderTime());
//            ordersListDto.setDues(o.getDues());
//            ordersListDto.setGoodsTotalPrice(o.getGoodsTotalPrice());
//            ordersListDto.setFreight(o.getFreight());
//            ordersListDto.setOrderState(o.getOrderState());
//            ordersListDto.setGoodsState(o.getGoodsState());
//            ordersListDto.setOrderType(o.getOrderType());
//            ordersListDto.setPaymentMode(o.getPaymentMode());
//            ordersListDto.setTransportMode(o.getTransportMode());
//            ordersListDto.setExpectedDate(o.getExpectedDate());
//            OrdersGoods ordersGoods = new OrdersGoods();
//            ordersGoods.setOrderId(o.getOrderId());
//            List<OrdersGoods> ordersGoodsList = ordersGoodsService.select(ordersGoods);
//            ordersListDto.setStockList(ordersUtil.getStockListByOrdersGoodsList(ordersGoodsList));
//            ordersListDtoList.add(ordersListDto);
//        }
//        PageInfo<OrdersListDto> selectPage = new PageInfo<>(ordersListDtoList);
//        Map<String, Object> result = new HashMap<>();
//        result.put("count", selectPage.getTotal());
//        result.put("ordersList", selectPage.getList());
//        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
//    }
}
